package leetcode.链表;

/**
 * @author: levi
 * @description: https://leetcode.cn/problems/merge-two-sorted-lists/
 * @date: 2022-9-14 14:24
 * @version: 1.0
 */
public class 合并两个有序链表_21 {
    public static void main(String[] args) {
        mergeTwoLists(null,null);
    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null) return list2;
        if(list2 == null) return list1;

        ListNode result = new ListNode(0);
        ListNode index = result;
        while (list1 != null && list2 != null){
            if(list1.val <= list2.val){
                index.next = list1;
                list1 = list1.next;
            }else if(list2.val < list1.val){
                index.next = list2;
                list2 = list2.next;
            }
            index = index.next;
        }
        if(list1 == null){
            index.next = list2;
        }
        if(list2 == null ){
            index.next = list1;
        }
        return result.next;
    }
}

class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) {
          this.val = val;
      }
      ListNode(int val, ListNode next) {
          this.val = val;
          this.next = next;
      }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}
